package com.llmcu.source.mybatisplus.officialsample.quickstart.shiro.config;

import com.llmcu.source.mybatisplus.officialsample.quickstart.shiro.realm.MyRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * xxxx
 *
 * @author liuling
 * @date 2024/12/30 20:59
 */
@Configuration
public class ShiroConfig {
    @Autowired
    private MyRealm myRealm;
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(){
        // 1.创建defaultWebSecurityManager对象
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        // 2.创建加密对象，设置相关属性
        HashedCredentialsMatcher hashMatcher = new HashedCredentialsMatcher();
        hashMatcher.setHashAlgorithmName("md5");
        hashMatcher.setHashIterations(3);
        // 3.将加密对象存储到myrealm中
        myRealm.setCredentialsMatcher(hashMatcher);
        // 4.myrealm存储到defaultWebSecurityManager
        defaultWebSecurityManager.setRealm(myRealm);
        // 5.返回defaultWebSecurityManager
        return defaultWebSecurityManager;
    }
    //配置shiro内置过滤器拦截范围
    @Bean
    public DefaultShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition shiroFilterChainDefinition = new DefaultShiroFilterChainDefinition();
        // 设置不认证可访问的资源
        shiroFilterChainDefinition.addPathDefinition("/user/login","anon");
        shiroFilterChainDefinition.addPathDefinition("/user/user-login","anon");
        // 设置需要认证可访问的资源
        shiroFilterChainDefinition.addPathDefinition("/**","authc");
        return shiroFilterChainDefinition;
    }
}
